<?php

namespace app\common\model;

use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\Hook;

class Readorder extends BaseModel
{

    protected $table = 'read_order';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';


    public static $status = [0 => '待支付', 3 => '已支付', 5 => '支付失败'];

    public static function getStatusText($val): string
    {
        return self::$status[$val] ?? '';
    }

    public static $payType = [0 => '待定', 1 => '微信', 2 => '支付宝'];

    public static function getPayTypeText($val): string
    {
        return self::$payType[$val] ?? '';
    }

    /**
     * 支付回调
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws Exception
     */
    public static function pay_notify($order_no)
    {
        $order = self::where('order_trade_no', $order_no)->find();

        if (!$order) {
            throw new Exception('订单不存在');
        }

        if (intval($order['pay_status']) === 3) {
            throw new Exception('订单已支付完毕');
        }

        $order->pay_status = '1';
        $order->paytime = time();
        $order->success_time = time();
        $order->save();

        return $order;
    }

    public static function checkOrder($args)
    {
        $orderInfo = self::where([
            'college_id' => $args['college_id'],
            'major_id' => $args['major_id'],
            'status' => 'normal'
        ])->find();
        return $orderInfo;
    }

}
